﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 连接
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        //修改数据
        private void button1_Click(object sender, EventArgs e)
        {
            //数据库连接串
            string connStr = "Data Source=.;Initial Catalog=sa;User ID=sa;Password=po89090";
            //创建SQLConnection的实例
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(connStr);
                //打开数据库连接
                conn.Open();
                //判断学生信息是否存在
                string checkNameSql = "select count(*) from student where name='{0}'";
                checkNameSql = string.Format(checkNameSql, textBox1.Text);
                //创建SqlCommand对象
                SqlCommand cmdCheckName = new SqlCommand(checkNameSql, conn);
                //执行SQL语句
                int isRepeatName = (int)cmdCheckName.ExecuteScalar();
                if (isRepeatName == 0)
                {
                    //通过姓名判断学生信息是否存在
                    MessageBox.Show("学生信息不存在！");
                    return;
                }

                string checkIdCodeSql = "select count(*) from student where idcode='{0}'";
                checkIdCodeSql = string.Format(checkIdCodeSql, textBox2.Text);
                //创建SqlCommand对象
                SqlCommand cmdCheckIdCode = new SqlCommand(checkIdCodeSql, conn);
                //执行SQL语句
                int isRepeatIdCode = (int)cmdCheckIdCode.ExecuteScalar();
                if (isRepeatIdCode == 0)
                {
                    //通过学号判断学生信息是否存在
                    MessageBox.Show("学生信息不存在！");
                    return;
                }
                //开始修改数据
                string sql = "select * from student where name='{0}' and idcode='{1}'";
                //填充SQL语句
                sql = string.Format(sql, textBox1.Text, textBox2.Text);
                //创建SqlDataAdapter类的对象
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                //创建DataSet类的对象
                DataSet ds = new DataSet();
                //使用SQLDataAdapter对象sda将查询结果填充到DataTable对象ds中
                sda.Fill(ds);
                if (ds.Tables[0].Rows.Count == 1)
                {
                    //判断新密码不能为空，以及判断两次输入的密码一致
                    if (!"".Equals(textBox3.Text) && textBox3.Text.Equals(textBox4.Text))
                    {
                        //创建SqlCommandBuilder类的对象
                        SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(sda);
                        //创建DataRow类的对象
                        DataRow dr = ds.Tables[0].Rows[0];
                        //设置idcode列的值
                        dr["idcode"] = textBox3.Text;
                        //更新数据库
                        sda.Update(ds);
                        //更新DataSet对象中的数据
                        ds.Tables[0].AcceptChanges();
                        MessageBox.Show("学号修改成功！");
                    }
                    else
                    {
                        MessageBox.Show("学号为空或者两侧输入的密码不一致！");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("学号修改失败！" + ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }

        //取消关闭
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
